home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / languages / cleo.lzh / Cleo / source / LEXFCT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-24  |  9.9 KB  |  404 lines

  1. /***************************************************************************
  2. *   Ce fichier, ainsi que tous les  modules  l'accompagnant, peut et  doit *
  3. * etre  copié GRATUITEMENT à la seule condition expresse de conserver      *
  4. * l'INTEGRALITE  du  Code Source, de  la documentation, et  des fichiers   *
  5. * annexes du package. Ce logiciel est Shareware, veuilez envoyer 100 FF à  *
  6. * l'auteur pour recevoir regulièrement les nouvelles versions.             *
  7. * Toute modification est INTERDITE sans l'autorisation écrite de l'auteur. *
  8. *            Tous droits réservés à M. DIALLO Barrou, Juillet 1992.        *
  9. ***************************************************************************/
  10.  
  11. /********            Fonctions de traitement de chaines         ********/
  12.  
  13. /* #define debug  */
  14. #define verbose
  15. #include <ctype.h>
  16. #ifdef msdos
  17.         #include "include\\cleobis.h"
  18.         #include "include\\libs.h"
  19. #else
  20.         #include "include/cleobis.h"
  21.         #include "include/libs.h"
  22. #endif
  23.  
  24. extern char curtoken[];
  25. extern CONST *symb;
  26. extern CONST *cursymb;
  27. extern VAR *var;
  28. extern VAR *curvar;
  29. extern int NbVar;
  30. extern MY_TYPESID curtokentype;
  31. extern MY_TYPESID lasttokentype;
  32. extern MY_CONST curconst;
  33. extern Entete head;
  34. extern void TraitErreur (char type, int num, int lig, int col);
  35. extern int curlg;
  36. extern int curcol;
  37. extern long AdressSize;
  38. extern long TextSize;
  39. extern long SymbolSize;
  40. extern long lg;
  41. extern int curtokenid;
  42. extern int lasttokenid;
  43. extern BOOLEEN Bool[];
  44. extern MOT Reserved[];
  45. extern OPE Mathope[];
  46. extern MOT Type[];
  47. extern MOT Fonctions[];
  48. extern MOT Math[];
  49. extern FCTLIB *extfct, *curextfct, *curlibfct;
  50.  
  51. #ifdef unix
  52.  
  53. int Stricmp(str1,str2)
  54. char *str1,*str2;
  55. {
  56.     int index = 0;
  57.  
  58.     while ( str1[index] && str2[index] &&
  59.             tolower(str1[index]) == tolower(str2[index]) )
  60.         ++index;
  61.  
  62.     return( (tolower(str1[index]) < tolower(str2[index])) ? -1 :
  63.           ( (tolower(str1[index]) > tolower(str2[index])) ?  1 : 0) );
  64. }
  65. #endif
  66.  
  67. /*** Fonction qui cherche le prochain chr
  68.         Renvoie le pointeur sur le chr suivant */
  69.  
  70. char *SearchChr(char *txt,char chr)
  71. {
  72.         char *tmp=txt, fin=0;
  73.         while(lg<TextSize && !fin)
  74.                 {
  75.                         if (*tmp==chr) fin=1;
  76.                         else
  77.                         {
  78.                                 if (*tmp=='\r' || *tmp=='\n')
  79.                                 {
  80.                                 curlg++;
  81. #ifdef verbose
  82.                     printf("# %d\r",curlg-1);
  83. #endif
  84.                                 }
  85.                                 tmp++; lg++;
  86.                         }
  87.                 }
  88.         if (fin) return(tmp+1);
  89.         else
  90.         {
  91.                 printf("Erreur: Fin de fichier Texte Prématurée\n");
  92.                 return((char*)NULL);
  93.         }
  94. }
  95.  
  96. char *ExtractLine(char *txt)
  97. {
  98.         return(SearchChr(txt,';'));
  99. }
  100.  
  101. /*** Fonction qui cherche la fin d'un commentaire
  102.                  Renvoie le pointeur sur le chr suivant */
  103. char *EndComment(char *txt)
  104. {
  105.         char *tmp=txt, fin=0;
  106.  
  107.         while( !fin && lg<TextSize)
  108.                 {
  109.                         if (*tmp=='*' && *(tmp+1)=='/') fin=1;
  110.                         else
  111.                         {
  112.                                 if (*tmp=='\r' || *tmp=='\n')
  113.                                 { curlg++;
  114. #ifdef verbose
  115.                     printf("# %d\r",curlg-1);
  116. #endif
  117.                                 }
  118.                                 tmp++; lg++;
  119.                         }
  120.                  }
  121.         if (fin) return(tmp+2);
  122.         else
  123.         {
  124.                 TraitErreur(RECERROR, BADEND,0,0);
  125.                 return((char*)NULL);
  126.         }
  127. }
  128.  
  129. char *Nombres(char *txt)
  130. {
  131.     int len=0, reel=0;
  132.     char *tmp=(char *)txt;
  133.     float res=0;
  134.     BOOL fin=FALSE;
  135.  
  136.     if (*txt =='-') { txt++; len++; lg++; }
  137.         while (!fin && (isnum(*txt) || (*txt=='-' && (*(txt-1)=='e' || *(txt-1)=='E')) ||
  138.                               (*txt=='+' && (*(txt-1)=='e' || *(txt-1)=='E')) ||
  139.                  *txt=='.' ||
  140.                 (*txt=='e' && (*(txt+1)=='-') ) ||
  141.                 (*txt=='e' && (isnum(*(txt+1))|| *(txt+1)=='+') ) ||
  142.                 (*txt=='E' && (*(txt+1)=='+') ) ||
  143.                 (*txt=='E' && (isnum(*(txt+1))|| *(txt+1)=='-') )))
  144.             {
  145.                 if (*txt=='.' && *(txt+1)=='.') fin++;
  146.                 else
  147.                 {
  148.                     if( *txt=='.' && *(txt+1) != '.') reel++;
  149.                     txt++; len++; lg++;
  150.                 }
  151.             }
  152.         if (reel >1)
  153.             {
  154.                 TraitErreur(FATALERROR, BADCONST,0,0);
  155.             }
  156.         else
  157.         if(reel==1)
  158.             res= Ascii2Reel(tmp,len);
  159.         else
  160.             res = Ascii2Entier(tmp,len);
  161.  
  162.         if ( (res-( (int)res)) == 0)
  163.             {
  164.              curtokentype = constint_mt;     /** c'est une Const Integer **/
  165.              curconst.integer = (long) res;
  166. #ifdef debug
  167.              printf("CONSTANTE=%d\tENTIER\n", curconst.integer);
  168. #endif
  169.             }
  170.         else
  171.             {
  172.              curtokentype = constreal_mt;       /** c'est une Const Real **/
  173.              curconst.real = (double)res;
  174. #ifdef debug
  175.             printf("CONSTANTE=%f\tFLOTTANT\n", curconst.real);
  176. #endif
  177.             }
  178. return(txt);
  179. }
  180.  
  181. char *Avance(char *txt)
  182. {
  183.     while (isspace(*txt))
  184.         {
  185.         if (*txt =='\r' || *txt =='\n')
  186.             { curlg++; curcol=0;
  187. #ifdef verbose
  188.                     printf("# %d\r",curlg-1);
  189. #endif
  190.  
  191.             }
  192.         txt++; lg++;
  193.         }
  194.     return(txt);
  195. }
  196.  
  197. char *ConstChr(char *txt)
  198. {
  199.     char *tmp = (char*)curtoken;
  200.     BOOL fin=FALSE;
  201.     *tmp = 0;   ++txt;
  202.  
  203.     while (!fin && lg < TextSize)
  204.          {
  205.  
  206.           if (*txt == '\'' && *(txt+1) == '\'')
  207.             {
  208.                *tmp = *txt++;
  209.                lg++;
  210.             }
  211.           else
  212.           {
  213.           if (*txt == '\'' && *(txt+1) != '\'') fin=TRUE;
  214.           else
  215.           if (*txt=='\r' || *txt=='\n')
  216.            {
  217.             curlg++;
  218.             #ifdef verbose
  219.                     printf("# %d\r",curlg-1);
  220.             #endif
  221.            }
  222.           }
  223.           if (!fin)
  224.             {
  225.             *tmp++ = *txt++;
  226.             lg++;
  227.             }
  228.          }
  229.     if (!(lg < TextSize))
  230.         TraitErreur(TEXTERROR, GUILLE, curlg, curcol);
  231.     *tmp =0;
  232.  
  233.     if (strlen(curtoken) > 1)
  234.         {
  235.         curtokentype = conststr_mt;      /** c'est une const chaine **/
  236.                     curtokenid = -1;
  237.         if (strlen(curtoken) < MAXSTRING)
  238.             strcpy( curconst.string, curtoken );
  239.         else
  240.             TraitErreur(TEXTERROR,STRING2LONG, curlg, curcol);
  241. #ifdef debug
  242.         printf("CONSTANTE=<%s>\tCONSTANTE CHAINE\n",curconst.string);
  243. #endif
  244.         }
  245.     else
  246.         {
  247.         curtokentype = constchr_mt;       /** c'est une const chr **/
  248.                     curtokenid = -1;
  249.         curconst.Char = *curtoken;
  250. #ifdef debug
  251.         printf("CONSTANTE=<%c>\tCONSTANTE CHR\n",curconst.Char);
  252. #endif
  253.         }
  254.  
  255.     lg++;
  256.     return(++txt);
  257. }
  258.  
  259.         /**** VRAI si curtoken est une Fonction Externe ****/
  260.  
  261. BOOL TestExtern()
  262. {
  263.     BOOL trouve=FALSE;
  264.     FCTLIB *cur=extfct;
  265.  
  266.     while (cur != NULL && !trouve)
  267.     {
  268.         if (!stricmp(curtoken, cur->nom))
  269.             trouve=TRUE;
  270.         else cur = cur->next;
  271.     }
  272.     if (cur != NULL)
  273.         {
  274.             curtokenid = extern_mt; /* Attention affectation du type
  275.                                          dans l'id pour Instruction()*/
  276.             curlibfct = (FCTLIB *)cur;
  277.             return(TRUE);
  278.         }
  279.     else
  280.         return(FALSE);
  281. }
  282.  
  283. /********** Fct qui renvoie VRAI si curtoken est un Mot Reservé Pascal ****/
  284.  
  285. BOOL TestMot(MOT *buf)
  286. {
  287.     BOOL fin=FALSE, trouve=FALSE;
  288.     int n=0;
  289.  
  290.     while (!fin && !trouve)
  291.     {
  292.         if (!stricmp(curtoken, buf[n].str))
  293.             trouve=TRUE;
  294.         else
  295.         if (buf[n].str==(char*)NULL)
  296.             fin=TRUE;
  297.         else n++;
  298.     }
  299.     if (trouve==TRUE)
  300.         {
  301.             curtokenid = (int) buf[n].id;
  302.             return(TRUE);
  303.         }
  304.     else
  305.         return(FALSE);
  306. }
  307. /********** Fct qui renvoie VRAI si curtoken est un Booleen Pascal ****/
  308.  
  309. BOOL TestBool(void)
  310. {
  311.     BOOL fin=FALSE, trouve=FALSE;
  312.     int n=0;
  313.  
  314.     while (!fin && !trouve)
  315.     {
  316.         if (!stricmp(curtoken, Bool[n].str))
  317.             trouve=TRUE;
  318.         else
  319.         if (Bool[n].str==(char*)NULL)
  320.             fin=TRUE;
  321.         else n++;
  322.     }
  323.     if (trouve==TRUE)
  324.         {
  325.             curtokenid = (int) Bool[n].id;
  326.             return(TRUE);
  327.         }
  328.     else
  329.         return(FALSE);
  330. }
  331.  
  332. char *Lettres(char *txt)
  333. {
  334.     char *tmp = (char*)curtoken;
  335.     *tmp = 0;
  336.  
  337.     while (alnum(*txt) || *txt=='_')
  338.          {
  339.             *tmp++ = *txt++;
  340.             lg++;
  341.          }
  342.     *tmp =0;
  343.  
  344. #ifdef debug
  345.     printf("TOKEN=%s\t", curtoken);
  346. #endif
  347.     if(TestMot(Math) == TRUE)
  348.             {
  349.              curtokentype = math_mt;           /** c'est une fct math. **/
  350. #ifdef debug
  351.              printf("Fonction Math\n");
  352. #endif
  353.             }
  354.     else
  355.     if(TestMot(Fonctions) == TRUE)
  356.             {
  357.              curtokentype = fonction_mt;           /** c'est une fonction. **/
  358. #ifdef debug
  359.              printf("Fonction\n");
  360. #endif
  361.             }
  362.     else
  363.     if(TestBool() == TRUE)
  364.             {
  365.             curtokentype = booleen_mt;           /** c'est un Booleen. **/
  366. #ifdef debug
  367.             printf("Booleen\n");
  368. #endif
  369.             }
  370.     else
  371.     if (TestMot(Type) == TRUE)
  372.         {
  373.          curtokentype = reservedtype_mt;     /** c'est un Type Reserve*/
  374. #ifdef debug
  375.          printf("Type Reservé\n");
  376. #endif
  377.         }
  378.     else
  379.     if (TestMot(Reserved) == TRUE)
  380.         {
  381.          curtokentype = reserved_mt;           /** c'est un mot reserve **/
  382. #ifdef debug
  383.          printf("Mot Reservé\n");
  384. #endif
  385.         }
  386.     else
  387.     if (TestExtern() == TRUE)
  388.         {
  389.          curtokentype = extern_mt;           /** c'est 1 fct externe **/
  390. #ifdef debug
  391.          printf("Fonction externe\n");
  392. #endif
  393.         }
  394.     else
  395.         {
  396.          curtokentype = ident_mt ;        /** c'est un Identificateur **/
  397.          curtokenid = ident_mt ;
  398. #ifdef debug
  399.          printf("Identificateur\n");
  400. #endif
  401.         }
  402.     return( txt);
  403. }
  404.